/* 8911106 302933981 Ofri Keidar */

/******************************************************************************
* Student Name: Ofri Keidar
* Exercise Name: Ex6
* File Description: Class MyInventoryException's implementation
******************************************************************************/


#include "MyInventoryException.h"

/***************************************************************************************
* function name: string intToString(const int intToCnvrt) const
* The Input: Int to be converted
* The output: String that its content is given number
* The Function operation: Starts from given number's right most digit, adds '0' to the
* 					      digit in order to get its ASCII value. Than adds this char
* 					      to result string. Keeps dividing by RADIX and repeats process
* 					      until no digits are left. Result string is returned
***************************************************************************************/
string MyInventoryException::intToString(const int intToCnvrt) const {

	int backup = intToCnvrt; // copy const input in order to change it
	string result = ""; // initialize result
	while (backup != 0) {
		char digit = backup % RADIX + '0'; // get ASCII of right most digit
		result = digit + result; // add ASCII of current digit to result string
		backup /= RADIX; // slice digit and move one digit to the left
	}
	return result; // return result string
}

/***************************************************************************************
* function name: char* stringToCharPtr(const string strToCnvrt)
* The Input: String to be converted to char* array
* The output: Char* array that points at a string with given string's content
* The Function operation: Deletes data previously pointed by m_errorMsg. Allocates a new
* 						  char* array (and assigns m_errorMsg to point to its end) which
* 						  fits given string's length. Copies all given string's characters
* 						  to new char* array and at the end add '\0'
***************************************************************************************/
char* MyInventoryException::stringToCharPtr(const string strToCnvrt) {

	delete[] m_errorMsg; // delete previously pointed data
	unsigned int stringLen = strToCnvrt.length()+1; // get string's length
	m_errorMsg = new char[stringLen+1]; // allocate array of chars that will store string's content
	unsigned int i; // initialize outside of loop in order to use it after loop's end

	// copy given string's each character to output
	for (i = 0; i < stringLen; ++i) {
		m_errorMsg[i] = strToCnvrt[i];
	}
	m_errorMsg[i] = '\0'; // end output
	return m_errorMsg; // return result
}

/***************************************************************************************
* function name: MyInventoryException(const MyInventoryException& excptToCopy)
* The Input: MyInventoryException object to be copied
* The output: None
* The Function operation: Creates a new MyInventoryException object and sets its m_errorMsg
* 					      to NULL
***************************************************************************************/
MyInventoryException::MyInventoryException(const MyInventoryException& excptToCopy): runtime_error(excptToCopy) {
	m_errorMsg = NULL;
}

/***************************************************************************************
* function name: MyInventoryException()
* The Input: None
* The output: None
* The Function operation: Creates a new MyInventoryException object and sets its m_errorMsg
* 					      to NULL
***************************************************************************************/
MyInventoryException::MyInventoryException(): runtime_error(DEFLT_MSG) {
	m_errorMsg = NULL;
}

/***************************************************************************************
* function name: virtual ~MyInventoryException() throw() = 0
* The Input: None
* The output: None
* The Function operation: Deletes m_errorMsg's pointed data
***************************************************************************************/
MyInventoryException::~MyInventoryException() throw() {
	delete[] m_errorMsg;
}

